package com.woniuxy.third.controller;

import com.woniuxy.third.annotation.RequirePermission;
import com.woniuxy.third.entity.Payments;
import com.woniuxy.third.entity.Teacher;
import com.woniuxy.third.entity.Teacherauth;
import com.woniuxy.third.entity.User;
import com.woniuxy.third.result.ResponseResult;
import com.woniuxy.third.service.PaymentsService;
import com.woniuxy.third.service.TeacherService;
import com.woniuxy.third.service.UserService;
import com.woniuxy.third.utils.FileUploadUtil;
import com.woniuxy.third.utils.GetUserUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: yangtao
 * @date 2021/7/19 10:03 @Version 1.0
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController {
    private static final Logger logger = LoggerFactory.getLogger(TeacherController.class);
    @Resource
    private TeacherService teacherService;
    @Resource
    private UserService userService;
    @Resource
    private PaymentsService paymentsService;

    @GetMapping("/findById")
    @ApiOperation(value = "查询教师", notes = "返回老师信息")
    public ResponseResult<Teacher> findById(HttpServletRequest request) {
        int id = GetUserUtil.getUser(request).getId();
        Teacher teacher = teacherService.findById(id);
        if (teacher != null) {
            return new ResponseResult<>(200, "查询成功", teacher);
        } else {
            return new ResponseResult<>(500, "教师信息不存在，请添加", null);
        }
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加老师信息", notes = "返回boolean，true成功，false失败")
    public boolean add(@RequestBody Teacher teacher, HttpServletRequest request) {
        int id = GetUserUtil.getUser(request).getId();
        teacher.setId(id);
        boolean isok = teacherService.addInfo(teacher);
        return isok;
    }

    @PostMapping("/updateImage")
    @ApiOperation(value = "添加头像", notes = "请选择上传照片,格式为jpg或者png返回对象信息")
    public ResponseResult<String> updateImage(HttpServletRequest request) {
        List<String> list = FileUploadUtil.multifileUpload(request);
        int id = GetUserUtil.getUser(request).getId();
        if (list.size() == 2) {
            if (list.get(1).equals("保存成功")) {
                teacherService.imageUpload(list.get(0), id);
                return new ResponseResult<>(200, list.get(1), null);
            }
            return new ResponseResult<>(500, list.get(1), null);
        }
        return new ResponseResult<>(500, "请上传一个文件", null);
    }

    @PostMapping("/updateQualification")
    @ApiOperation(value = "上传资质照片", notes = "请选择上传照片,格式为jpg或者png返回对象信息")
    public ResponseResult<String> updateQualification(HttpServletRequest request) {
        List<String> list = FileUploadUtil.multifileUpload(request);
        int id = GetUserUtil.getUser(request).getId();

        if (list.get(list.size() - 1).equals("保存成功")) {
            System.out.println(list.get(list.size() - 1));
            teacherService.updateQualification(list.get(0), id);
            return new ResponseResult<>(200, "修改成功", null);
        } else {
            return new ResponseResult<>(500, list.get(list.size() - 1), null);
        }
    }

    @PostMapping("/updatePhone")
    @ApiOperation(value = "更改手机号码", notes = "返回对象信息")
    @ApiImplicitParam(paramType = "query", name = "phone", value = "手机号码", required = true)
    public ResponseResult<String> updatePhone(@RequestBody User teacher, HttpServletRequest request) {
        int tid = GetUserUtil.getUser(request).getId();
        String phone = teacher.getPhone();
        int i = teacherService.updatePhone(phone, tid);
        if (i == 1) {
            return new ResponseResult<>(200, "修改成功", null);
        }
        return new ResponseResult<>(500, "修改失败", null);
    }

    @PostMapping("/updateIntro")
    @ApiOperation(value = "简介", notes = "返回对象信息")
    @ApiImplicitParam(paramType = "query", name = "intro", value = "简介", required = true)
    public ResponseResult<String> updateIntro(String intro, HttpServletRequest request) {
        int tid = GetUserUtil.getUser(request).getId();
        int i = teacherService.updateIntro(intro, tid);
        if (i == 1) {
            return new ResponseResult<>(200, "修改成功", null);
        }
        return new ResponseResult<>(500, "修改失败", null);
    }

    @PostMapping("/delinfo")
    @ApiOperation(value = "删除teacher信息", notes = "返回更改信息是否成功")
    public ResponseResult<String> updateIntro(HttpServletRequest request) {
        int id = GetUserUtil.getUser(request).getId();
        int i = teacherService.delinfo(id);
        return new ResponseResult<>(200, "修改成功", null);
    }

    @PostMapping("/updatepayment")
    @ApiOperation(value = "添加支付信息", notes = "返回更改信息是否成功")
    public ResponseResult<String> updatepayment(@RequestBody Payments payments, HttpServletRequest request) {
        int uid = GetUserUtil.getUser(request).getId();
        Payments paymentS = paymentsService.findByUid(uid);
        Payments payinfo = payments.setUid(uid);
        if (paymentS != null) {
            paymentsService.updateByUid(payinfo);
            return new ResponseResult<>(200, "修改成功", null);
        }else{
            paymentsService.addinfo(payinfo);
            return new ResponseResult<>(200, "修改成功", null);
        }
    }

    @PostMapping("/updateauth")
    @ApiOperation(value = "添加认证信息", notes = "返回更改信息是否成功")
    public ResponseResult<String> updateauth(@RequestBody Teacherauth teacherauth, HttpServletRequest request) {
        int uid = GetUserUtil.getUser(request).getId();
        teacherauth.setId(uid);
        teacherService.updateauth(teacherauth);
        return new ResponseResult<>(200, "修改成功", null);
    }

    @PostMapping("/updateinfo")
    @ApiOperation(value = "添加认证信息", notes = "返回更改信息是否成功")
    public ResponseResult<String> updateinfo(@RequestBody Teacher teacher, HttpServletRequest request) {
        System.out.println(teacher);
        int uid = GetUserUtil.getUser(request).getId();
        teacher.setId(uid);
        teacherService.updateinfo(teacher);
        return new ResponseResult<>(200, "修改成功", null);
    }


    }
